package com.ps.util;

import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.regex.Pattern;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.validator.routines.IntegerValidator;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ps.entity.Result;


public class Kit {
	private final static Serializer serializer = new Persister();
	private static final Gson JSON_CONVERTER = new Gson(); 

	public static final IntegerValidator INTEGER_VALIDATOR = new IntegerValidator();
	private static final Gson JSON_CONVERTER_FIELDSWITHOUT = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); 
	
	private static final JsonConfig jsonConfig= new JsonConfig();   
	static {
		//jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); 
		jsonConfig.setExcludes(new String[]{"handler","hibernateLazyInitializer"});   
	}
	
	
	public static String obj2XmlString(Object o) {
		ByteArrayOutputStream out = new  ByteArrayOutputStream ();
		try {
			serializer.write(o, out);
			return out.toString("utf-8");

		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return "";
	}  
	
	public static <T> T xmlString2Obj(Class<T> classzz, String xml) {
		try {
			return serializer.read(classzz, xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}  	
	
	public static <T> T xmlString2Obj(Class<T> classzz, InputStream source) {
		try {
			return serializer.read(classzz, source);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	} 		
	
	public static String toJson(Object o) {
		return JSON_CONVERTER.toJson(o);
	}
	
	
	public static String toJsonOfLib(Object o) {
		return JSONObject.fromObject(o, jsonConfig).toString();
	}
	
	public static String toJsonFieldsWithout(Object o) {
		return JSON_CONVERTER_FIELDSWITHOUT.toJson(o);
	}
	
	public static <T> T getFromJson(String value, Class<T> cls) {
		
		if (StringUtils.isNotBlank(value)) {
			try {
				return JSON_CONVERTER.fromJson(value, cls);
			} catch (Exception e) {
				System.out.println("JsonSyntaxException " + e.getCause());
			}
		}
		return null;
	}
	
	
	
	
	public static <T> T getFromJson(String value, Type type) {
		
		if (StringUtils.isNotBlank(value)) {
			try {
				return JSON_CONVERTER.fromJson(value, type);
			} catch (Exception e) {
				System.out.println("JsonSyntaxException " + e.getCause());
			}
		}
		return null;
	}	
	
	
    
	
	
	/**
	 * ��ָ�����ȷָ��ַ�,ͨ�����ڶ���
	 * @param msg
	 * @param num
	 * @return
	 */
	public static String[] split(String msg, int num) {
		int len = msg.length();
		if (len <= num)
			return new String[] { msg };

		int count = len / (num - 1);
		count += len > (num - 1) * count ? 1 : 0; // ����Ӧ��ֵ��ע��

		String[] result = new String[count];

		int pos = 0;
		int splitLen = num - 1;
		for (int i = 0; i < count; i++) {
			if (i == count - 1)
				splitLen = len - pos;

			result[i] = msg.substring(pos, pos + splitLen);
			pos += splitLen;

		}

		return result;
	}
	/**
	 * ��ָ������ʹ��������ʽ�ָ��ַ�,ͨ�����ڶ���
	 * @param msg
	 * @param num
	 * @return
	 */
	public static String[] splitPattern(String msg, int num) {
                
		Pattern p = Pattern.compile("(?<=\\G.{" + num + "})(?!$)"); 
		return p.split(msg);

	}
	
	public static Result createResult() {
		Result reslut = new Result();
		reslut.setStatus(new com.ps.entity.Status());
		return reslut;
	}
}
